package demoMod.invincibleOne.cards.invincible;

import basemod.helpers.CardModifierManager;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.vfx.TextAboveCreatureEffect;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.MonsterRegenOrMaxHPApplyHook;
import demoMod.invincibleOne.interfaces.SpawnMonsterHook;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;
import demoMod.invincibleOne.relics.TaskGoal;

public class TouchingSpeech extends AbstractInvincibleCard implements SpawnMonsterHook {
    public static final String ID = InvincibleOneMod.makeID("TouchingSpeech");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/TouchingSpeech.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.COMMON;
    private static final CardTarget TARGET = CardTarget.ALL_ENEMY;

    private static final int COST = -2;

    public TouchingSpeech() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 25;
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
        this.tags.add(CardTagsEnum.ALERT);
    }

    @Override
    public boolean onBattleStart() {
        TaskGoal.AddToAlertQueue(35, new TaskGoal.AlertLambda() {
            @Override
            public void run() {
                for (AbstractMonster m : AbstractDungeon.getMonsters().monsters) {
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            float tmp = TouchingSpeech.this.magicNumber;
                            for (AbstractPower power : m.powers) {
                                if (power instanceof MonsterRegenOrMaxHPApplyHook) {
                                    MonsterRegenOrMaxHPApplyHook hook = (MonsterRegenOrMaxHPApplyHook) power;
                                    tmp = hook.onMaxHpIncreased(m, tmp);
                                }
                            }
                            for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                                if (card instanceof MonsterRegenOrMaxHPApplyHook) {
                                    tmp = ((MonsterRegenOrMaxHPApplyHook) card).onMaxHpIncreased(m, tmp);
                                }
                            }
                            m.maxHealth += Math.ceil(tmp);
                            AbstractDungeon.effectsQueue.add(new TextAboveCreatureEffect(m.hb.cX - m.animX, m.hb.cY, AbstractCreature.TEXT[2] + (int) tmp, Settings.GREEN_TEXT_COLOR));
                            m.healthBarUpdatedEvent();
                            isDone = true;
                        }
                    });
                }
            }
        });
        return true;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> upgradeMagicNumber(10);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void onSpawnMonster(AbstractMonster m) {
        TaskGoal.AddToAlertQueue(35, new TaskGoal.AlertLambda() {
            @Override
            public void run() {
                float tmp = TouchingSpeech.this.magicNumber;
                for (AbstractPower power : m.powers) {
                    if (power instanceof MonsterRegenOrMaxHPApplyHook) {
                        MonsterRegenOrMaxHPApplyHook hook = (MonsterRegenOrMaxHPApplyHook) power;
                        tmp = hook.onMaxHpIncreased(m, tmp);
                    }
                }
                for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                    if (card instanceof MonsterRegenOrMaxHPApplyHook) {
                        tmp = ((MonsterRegenOrMaxHPApplyHook) card).onMaxHpIncreased(m, tmp);
                    }
                }
                m.maxHealth += Math.ceil(tmp);
                AbstractDungeon.effectsQueue.add(new TextAboveCreatureEffect(m.hb.cX - m.animX, m.hb.cY, AbstractCreature.TEXT[2] + (int) tmp, Settings.GREEN_TEXT_COLOR));
                m.healthBarUpdatedEvent();
            }
        });
    }
}
